package com.ronri.ams.task;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.ronri.ams.bean.taskListBean;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 文件：ams.java
 * 包名：com.ronri.ams.task;
 * 说明：
 * 作者：MS During
 * 日期：2017/5/24
 * 历史：
 */
public class SimpleAdapterDetailWithEvent extends BaseAdapter {

    // 外部接口事件
    private OnControlClickEvent mEvents = null;

    // 需要被监控的ID
    private int[] mMonitorID;

    // 句柄
    private Context mContext = null;

    private taskListBean mtaskListBean = null;

    private int mResource = 0;

    private List<Map<String, Object>> mItems = new ArrayList<>();
    /***
     * @param context
     * @param events
     * @param onEvent
     */
    public SimpleAdapterDetailWithEvent(Context context, int nResource, List<Map<String, Object>> data, int[] events, OnControlClickEvent onEvent) {
        mContext = context;
        mMonitorID = events;
        mEvents = onEvent;
        mItems = data;
        mResource = nResource;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);

            holder = new ViewHolder(convertView);

            convertView.setTag(holder);

            //对于listview，注意添加这一行，即可在item上使用高度
            AutoUtils.autoSize(convertView);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (mMonitorID.length > 0) {
            Map<String, Object> item =  mItems.get(position);

            holder.tvDate.setText(item.get("date").toString().replace("null", ""));
            holder.tvDept.setText(item.get("dept").toString().replace("null", ""));
            holder.tvSampleName.setText(item.get("samplename").toString().replace("null", ""));
            holder.tvUser.setText(item.get("user").toString().replace("null", ""));

            //holder.tvTaskSampleName.setText(bean.getBreed_name());
            //holder.tvTaskTitle.setText(bean.getTask_name());

            for (int i = 0; i < mMonitorID.length; i++) {
                View iTem = convertView.findViewById(mMonitorID[i]);
                iTem.setTag(item.get("positon"));
                iTem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mEvents != null) {
                            mEvents.OnClick(v, Integer.valueOf(v.getTag().toString()));
                            //notifyDataSetChanged();
                        }
                    }
                });
            }
        }

        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.tvDept)
        TextView tvDept;
        @BindView(R.id.tvUser)
        TextView tvUser;
        @BindView(R.id.tvDate)
        TextView tvDate;
        @BindView(R.id.tvSampleName)
        TextView tvSampleName;

        ViewHolder(View view) {
            tvDept = (TextView)view.findViewById(R.id.tvDept);
            tvUser = (TextView)view.findViewById(R.id.tvUser);
            tvDate = (TextView)view.findViewById(R.id.tvDate);
            tvSampleName = (TextView)view.findViewById(R.id.tvSampleName);
        }
    }
}
